//
//  ViewController.m
//  Metal-Master
//
//  Created by 杨冰冰 on 2020/6/15.
//  Copyright © 2020 杨冰冰. All rights reserved.
//

#import "ViewController.h"
#import "BaseViewController.h"
//第一组
#import "Calculations_on_a_CPU.h"
#import "Draw_a_view.h"
#import "Render_primitives.h"
#import "SyncCPU_and_GPU.h"
#import "TextureSampling.h"
//第二组

//其他
#define kDataItemKeyName @"DataItemKeyName"
#define kDataItemClass @"DataItemClass"

#define kDataSourceGroupName @"DataSourceGroupName"
#define kDataSourceGroupList @"DataSourceGroupList"
#import <CloudKit/CloudKit.h>
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSArray *dataSource;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataSource = @[
        @{
            kDataSourceGroupName : @"Basic Tasks and Concepts",
            kDataSourceGroupList : @[
            @{kDataItemKeyName : @"Calculations on a CPU", kDataItemClass : [Calculations_on_a_CPU class]},
            @{kDataItemKeyName : @"Draw a view", kDataItemClass : [Draw_a_view class]},
            @{kDataItemKeyName : @"Render primitives", kDataItemClass : [Render_primitives class]},
            @{kDataItemKeyName : @"SyncCPU_and_GPU", kDataItemClass : [SyncCPU_and_GPU class]},
            @{kDataItemKeyName : @"TextureSampling", kDataItemClass : [TextureSampling class]},

            ]},
//        @{
//            kDataSourceGroupName : @"others",
//            kDataSourceGroupList : @[
//            @{kDataSourceKeyName : @"Calculations_on_a_CPU", kDataSourceClass : [Calculations_on_a_CPU class]},
//            ]},
        
        
        
        //TODo
    ];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    
    // Do any additional setup after loading the view.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *itemList = [self.dataSource[section] objectForKey:kDataSourceGroupList];
    return itemList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
    }
    NSArray *itemList = [self.dataSource[indexPath.section] objectForKey:kDataSourceGroupList];
    NSDictionary *item = [itemList objectAtIndex:indexPath.row];
    cell.textLabel.text = [item objectForKey:kDataItemKeyName];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *itemList = [self.dataSource[indexPath.section] objectForKey:kDataSourceGroupList];
    NSDictionary *item = [itemList objectAtIndex:indexPath.row];
    Class viewClass = [item objectForKey:kDataItemClass];
    BaseView *view = (BaseView *)[[viewClass alloc] initWithFrame:self.view.bounds];
    BaseViewController *controller = [[BaseViewController alloc] init];
    controller.contentView = view;
    [self.navigationController pushViewController:controller animated:YES];
    
}
@end
